import 'package:t3_data_service/biz_data_source/map/address/city_pick/model/city_pick_models.dart';
import 'package:t3_data_service/biz_data_source/map/address/poi_search/poi_search.dart';
import 'package:t3_data_service/data_provider/map_sctx/t3_map_sctx_provider.dart';
import 'address_select_enums.dart';
import 'address_select_config.dart';
import 'package:t3_foundation/t3_foundation.dart';

enum PageScene {
  normal(0),
  commonAddress(1),
  feedback(2),
  mapSelectSearch(3);

  final int value;
  const PageScene(this.value);

  static PageScene fromJson(dynamic value) {
    if (value is int) {
      if (value == 1) {
        return PageScene.commonAddress;
      }
      if (value == 2) {
        return PageScene.feedback;
      }
      if (value == 3) {
        return PageScene.mapSelectSearch;
      }
    }
    return PageScene.normal;
  }
}

/// POI搜索的基类
/// 继承自选址基类
class POISearchBaseConfig extends AddressSelectConfig {
  /// 家、公司、图选入口配置
  List<T3EntryOptions> entryOptions = [];
  /// 是否自动打开选择城市, 默认false
  /// 起点搜索时，还没有城市信息的时候用的，打开城市选择再进行搜索，用南京作为默认城市
  bool autoOpenCitySelection = false;
  /// 退出页面到route，不配置默认走pop()
  /// 暂时无用：TODO：要改下原生代码试下
  String? popUntilRoute;
  PageScene pageScene = PageScene.normal;

  POISearchBaseConfig({
    required super.location,
    super.originCoor,
    super.sceneRuleId,
    super.refererPageTitle,
    super.allowMoveOutFence = true,
    super.pointType = T3POIType.start,
    super.isUserActionDisabledForEnd = false,
    super.isUserActionDisabledForVia = false,
    super.needHideAddMidwayButton = false,
    super.style = T3POIStyle.normal,
    super.cityScope = CityScope.opened,
    super.showMapSelectedFooter = false,
    super.showConfusionButton = true,
    super.showBackButton = false,
    super.shouldCheckTableSelectedPoi = true,
    this.entryOptions = const [],
    this.autoOpenCitySelection = false,
    this.popUntilRoute,
    this.pageScene = PageScene.normal,
  });

  bool get showHotPercent {
    return pageScene != PageScene.commonAddress;
  }

  static POISearchBaseConfig fromJson(Map<dynamic, dynamic> json) {
    var config = POISearchBaseConfig(
      location: $T3POILocationDecode(json['location']),
      originCoor: LatLng.fromJson(json['originCoor']),
      allowMoveOutFence: BoolUtils.dynamicToBool(json['allowMoveOutFence'], true),
      pointType: T3POIType.fromJson(IntUtils.dynamicToNullInt(json['pointType'])),
      isUserActionDisabledForEnd: BoolUtils.dynamicToBool(json['isUserActionDisabledForEnd']),
      isUserActionDisabledForVia: BoolUtils.dynamicToBool(json['isUserActionDisabledForVia']),
      needHideAddMidwayButton: BoolUtils.dynamicToBool(json['needHideAddMidwayButton']),
      style: T3POIStyle.fromJson(IntUtils.dynamicToNullInt(json['style'])),
      cityScope: CityScopeExtension.initWithRawValue(IntUtils.dynamicToNullInt(json['cityScope'])) ?? CityScope.opened,
      showMapSelectedFooter: BoolUtils.dynamicToBool(json['showMapSelectedFooter']),
      showConfusionButton: BoolUtils.dynamicToBool(json['showConfusionButton'],true),
      showBackButton: BoolUtils.dynamicToBool(json['showBackButton']),
      sceneRuleId: StringUtils.dynamicToString(json['sceneRuleId']),
      refererPageTitle: StringUtils.dynamicToString(json['refererPageTitle']),
      shouldCheckTableSelectedPoi: BoolUtils.dynamicToBool(json['shouldCheckTableSelectedPoi'], true),
      entryOptions: $T3EntryOptionsEnumDecode(IntUtils.dynamicToNullInt(json['entryOptions'])),
      autoOpenCitySelection: BoolUtils.dynamicToBool(json['autoOpenCitySelection'], false),
      popUntilRoute: StringUtils.dynamicToString(json['popUntilRoute']),
      pageScene: PageScene.fromJson(json['pageScene']),
    );
    config.mapType = T3MapType.fromJson(IntUtils.dynamicToNullInt(json['mapType']));
    return config;
  }

  @override
  Map<String, dynamic> toJson() {
    var json = super.toJson();
    var entryOptionsInt = 0;
    if (entryOptions.contains(T3EntryOptions.homeAndCompany)) {
      entryOptionsInt = 1;
    }
    if (entryOptions.contains(T3EntryOptions.map)) {
      entryOptionsInt += 2;
    }
    json['entryOptions'] = entryOptionsInt;
    json['autoOpenCitySelection'] = autoOpenCitySelection;
    json['popUntilRoute'] = popUntilRoute;
    json['pageScene'] = pageScene.value;
    return json;
  }
}